const {
    url,
    dbname,
    collections
} = require("./config")

function execCreate() {}

function execRetrieve(collectionName, whereOption = {}) {

    const {
        MongoClient
    } = require("mongodb")
    // const url = "mongodb://localhost:27017/";
    return new Promise((resolve, reject) => {
        MongoClient.connect(url,
            function (err, db) {

                // if (err) throw err;
                if (err) {
                    reject(err)
                } else {
                    const dbo = db.db(dbname);
                    dbo.collection(collectionName)
                        .find(whereOption)
                        .toArray(function (err, result) {
                            db.close(); // 返回集合中所有数据
                            if (err) {
                                reject(err)
                            } else {
                                // console.log("db execRetrieve:result", result);
                                resolve(result)

                            }


                        });
                }

            }
        );
    })


}

function execUpdate() {

}

function execDelete() {

}

module.exports = {
    execCreate,
    execRetrieve,
    execUpdate,
    execDelete
}